package com.hnrd.crud.controller;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hnrd.crud.annotation.Permissions;
import com.hnrd.crud.mapper.RolePermissionMapper;
import com.hnrd.crud.mapper.TRoleMapper;
import com.hnrd.crud.pojo.Permission;
import com.hnrd.crud.pojo.RolePermission;
import com.hnrd.crud.pojo.TRole;

@RequestMapping("role")
@RestController
public class RoleController {
	@Autowired
	private TRoleMapper tRoleMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	
	@RequestMapping("addRole")
	public ResponseEntity<Map<String, Object>> addRole(TRole role){//添加角色
		Map<String, Object> map=new HashMap<>();
		map.put("msg", "添加角色成功！");
		try {
			tRoleMapper.insert(role);
			return new ResponseEntity<>(map,HttpStatus.OK);
		} catch (Exception e) {
			map.put("msg","角色添加失败！");
			return new ResponseEntity<>(map,HttpStatus.BAD_GATEWAY);
		}	
	}
	@Permissions(permission={"管理员权限"})
	@RequestMapping("selectAllRoles")
	public ResponseEntity<List<TRole>> selectAllRoles(){//查询所有角色
		List<TRole> roles = tRoleMapper.selectAll();
		return new ResponseEntity<>(roles,HttpStatus.OK);
	}
	
	@RequestMapping("addRolePermissons")
	public ResponseEntity<Map<String, Object>> addRolePermissions(@RequestBody List<RolePermission> rolePermissions){//添加角色的权限
		Map<String, Object> map=new HashMap<>();
		map.put("msg", "添加权限成功！");
		try {
			rolePermissionMapper.addRolePermissions(rolePermissions);
			return new ResponseEntity<>(map,HttpStatus.OK);
		} catch (Exception e) {
			map.put("msg", "添加角色权限失败！");
			return new ResponseEntity<>(map,HttpStatus.BAD_GATEWAY);
		}
	}
	@RequestMapping("selectRolePermissions")
	public ResponseEntity<List<Permission>> selectRolePermissions(Integer roleId){//查看角色权限
		List<Permission> list= rolePermissionMapper.selectRolePermissions(roleId);
		return new ResponseEntity<>(list,HttpStatus.OK);
	}
	
	@RequestMapping("deleteRolePermissions")
	public ResponseEntity<Map<String, Object>> deleteRolePermissions(Integer[] permissions){//删除角色的权限
		return null;
	}
	
	
	
}
